import pymysql
import random
import json

mysql_conf = {
    "host": "localhost",
    "user": "root",
    "password": "qwe369",
    "db": "tunan_class"
}

con_local = pymysql.connect(**mysql_conf)

# 实际操作都需要光标进行
cursor_local = con_local.cursor()


def transaction_test():
    try:
        SQL_1 = "INSERT INTO tunan_class(name, class) values ('name_new', 'houlang');"
        SQL_2 = "INSERT INTO houlang(name) values ('name_new');"
        cursor_local.execute(SQL_1)
        cursor_local.execute(SQL_2)
    except Exception as e:
        print(e.args)
        con_local.rollback()
    finally:
        con_local.commit()


if __name__ == "__main__":
    transaction_test()
